package ui.tp3.TP3Wicket;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.FeedbackPanel;

import ui.tp3.modelo.UserException;

public abstract class FeedbackAjaxButton extends AjaxButton {
	private final FeedbackPanel feedbackPanel;

	public FeedbackAjaxButton(String id, FeedbackPanel feedbackPanel) {
		super(id);
		this.feedbackPanel = feedbackPanel;
	}

	protected void onError(AjaxRequestTarget target, Form<?> arg1) {
		target.add(this.feedbackPanel);
	}

	protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
		try {
			this.doOnSubmit(target, form);
		} catch (UserException e) {
			feedbackPanel.error(e.getMessage());
			target.add(feedbackPanel);
		}

	}

	protected abstract void doOnSubmit(AjaxRequestTarget target, Form<?> form);

}
